import store from '@/store'

function checkUserType(el, binding) {
  const { value } = binding
  const userType = store.getters.userType

  if (value && value instanceof Array && value.length > 0) {
    const hasUserType = value.includes(userType)

    if (!hasUserType) {
      el.parentNode && el.parentNode.removeChild(el)
    }
  } else {
    throw new Error(`need userTypes! Like v-hasUserType="[5,4,3]"`)
  }
}

export default {
  inserted(el, binding) {
    checkUserType(el, binding)
  },
  update(el, binding) {
    checkUserType(el, binding)
  }
}
